import { getPipelineConfig } from '@/api/pipeline';
import { defineStore } from 'pinia';

export const usePipelineStore = defineStore('pipeline', {
  state: (): Pipeline.PipelineConfig => {
    return {
      taskList: <Pipeline.Task[]>[], // 流程编排数据
      baseInfo: <Pipeline.BaseInfo>{}, // 基础信息数据
      triggerSetting: <Pipeline.TriggerSetting>{}, // 触发器设置数据
      variableSettingList: <Pipeline.VariableSetting[]>[], // 变量数据
      cacheSettingList: <Pipeline.CacheSetting[]>[], // 缓存数据
    };
  },
  getters: {
    getFlowChoreographyData(): Pipeline.Task[] {
      return this.taskList ?? [];
    },
    getBaseInfoData(): Pipeline.BaseInfo {
      return this.baseInfo ?? {};
    },
    getTriggerSettingsData(): Pipeline.TriggerSetting {
      return this.triggerSetting ?? {};
    },
    getVariablesData(): Pipeline.VariableSetting[] {
      return this.variableSettingList ?? [];
    },
    getCacheData(): Pipeline.CacheSetting[] {
      return this.cacheSettingList ?? [];
    },
    getAllData(): Pipeline.PipelineConfig {
      return {
        taskList: this.taskList,
        baseInfo: this.baseInfo, // 基础信息数据
        triggerSetting: this.triggerSetting, // 触发器设置数据
        variableSettingList: this.variableSettingList, // 变量数据
        cacheSettingList: this.cacheSettingList,
      };
    },
  },
  actions: {
    setFlowChoreographyData(info: any) {
      this.taskList = [...info];
    },
    setBaseInData(info: any) {
      this.baseInfo = { ...info };
    },
    setTriggerSettingsData(info: any) {
      this.triggerSetting = { ...info };
    },
    setVariablesData(info: any) {
      this.variableSettingList = [...info];
    },
    setCacheDataData(info: any) {
      this.cacheSettingList = [...info];
    },
    async fetchPipelineConfigData(code: string) {
      const data = await getPipelineConfig(code);
      if (data) {
        this.taskList = data.taskList;
        this.baseInfo = data.baseInfo || {};
        this.cacheSettingList = data.cacheSettingList || [];
        this.variableSettingList = data.variableSettingList || [];
        this.triggerSetting = data.triggerSetting || {};
      }
    },
  },
  // 配置是否存入本地缓存，默认存入sessionStorage，具体使用参考文档 https://seb-l.github.io/pinia-plugin-persist/
  persist: {
    enabled: true,
  },
});
